home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
batchut
/
ask3.zip
/
ASK3.C
< prev
next >
Wrap
C/C++ Source or Header
|
1986-12-11
|
15KB
|
622 lines
/* Copyright 1986 Academic Computing Center, University of Wisconsin - Madison
**
** ASK to prompt the user to enter a character. Result is a value 0-255
** testable in a batch file with the "if errorlevel" construct.
** Compile with /ze/ox options.
**
** Written by Peter Wu for the Faculty Support Center.
*/
#define LINT_ARGS
#define SWITCHC '/'
#define SPECIAL '\\' /* the escape character */
#define CONTROL '~' /* control character prefix */
#define XOPN '(' /* open quote for extended ascii e.g. ~(home) */
#define XCLS ')'
#define acc(seg,off) ((long)(seg)<<16|(unsigned short)(off))
#define peekb(seg,off) (*(unsigned char far *)acc(seg,off))
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <stdlib.h>
#include <dos.h>
#include <memory.h>
char
*xasc[] = { /* mnemonics for extended ascii */
"", "", "", "NULL?", "", "", "", "", "", "", "", "", "", "", "",
"S-Tab", "A-Q", "A-W", "A-E", "A-R", "A-T", "A-Y", "A-U", "A-I", "A-O",
"A-P", "", "", "", "", "A-A", "A-S", "A-D", "A-F", "A-G", "A-H", "A-J",
"A-K", "A-L", "", "", "", "", "", "A-Z", "A-X", "A-C", "A-V", "A-B",
"A-N", "A-M", "", "", "", "", "", "", "", "", "F1", "F2", "F3", "F4",
"F5", "F6", "F7", "F8", "F9", "F10", "", "", "Home", "Up", "PgUp", "",
"Left", "", "Right",
"", "End", "Down", "PgDn", "Ins", "Del", "S-F1", "S-F2", "S-F3",
"S-F4", "S-F5", "S-F6", "S-F7", "S-F8", "S-F9", "S-F10", "C-F1", "C-F2",
"C-F3", "C-F4", "C-F5", "C-F6", "C-F7", "C-F8", "C-F9", "C-F10", "A-F1",
"A-F2", "A-F3", "A-F4", "A-F5", "A-F6", "A-F7", "A-F8", "A-F9", "A-F10",
"C-PrtSc", "C-Left", "C-Right", "C-End", "C-PgDn", "C-Home", "A-1", "A-2",
"A-3",
"A-4", "A-5", "A-6", "A-7", "A-8", "A-9", "A-0", "A--", "A-=", "C-PgUp"
};
char *xi();
char *xget();
int ctrlc();
_setenvp(){} /* diable code dealing with enironment variables */
_nullcheck() /* disable null pointer checking */
{
return 0; /* this line is required */
}
main()
{
char
argvbuf[128], /* string space for storing parameter */
*argv[65], /* max # of parameters that fits on a line */
*prompt, /* prompt string */
*option, /* option string */
*tmp,
*nulls = "";
int
argc,
optf, /* 0 = no user options on cmd line; 1 = yes */
quiet, /* 0 = enable error message; 1 = quiet */
flead, /* flush type ahead flag 1=flush */
echo,
index,
timeout, /* timeout value in seconds */
timeflag, /* whether timeout options is set or not */
where,
i,
cases; /* 0 = case non-sensitive; 1 = case sensitive */
unsigned int
expect[200], /* expected response string */
c;
long
expire; /* expire time = start time + timeout */
getarg(&argc, argv, argvbuf); /* my routine to do argc, argv */
if (argc == 1) { /* no argument */
cputs("ASK version 3.0 (Nov 21, 1986) pre-release\15\n\n");
cputs("Usage: ASK[/cefmqs] [prompt] [expected response]\15\n");
cputs("/c - case sensitive /m### - timeout in ### minutes\15\n");
cputs("/e - no echo /q - accept unexpected key\15\n");
cputs("/f - flush type-ahead /s### - timeout in ### seconds\15\n");
cputs("E.G. (in batch file):\15\n");
cputs(" ASK \"Yes or No? \" yn\15\n");
cputs(" if errorlevel 2 goto NO\15\n");
exit(0);
}
/* set default options */
quiet = 0; /* not quiet; i.e. beeps on unexpected input */
cases = 0; /* not case sensitive; i.e. a == A */
echo = 1; /* echo input */
flead = 0; /* no flush type-ahead, user can type ahead if he wants */
timeout = -1; /* default timeout is immediate */
timeflag = 0; /* timeout option not enabled */
if (*argv[1] == SWITCHC) { /* check for option string */
option = argv[1] + 1;
optf = 1; /* remember to shift prompt and expect */
c = *option;
while (c) {
option++;
switch (c) {
case SWITCHC: case ' ':
break; /* ignore extra switch char and space */
case 'c': case 'C':
cases = 1; /* set case sensitive */
break;
case 'e': case 'E': /* no echo option */
echo = 0;
break;
case 'f': case 'F': /* flush type ahead */
flead = 1; /* the actual flushing is done below */
break;
case 's': case 'S': /* timeout in seconds */
tmp = xi(option, &i); /* read timeout value (in seconds) */
if (tmp > option) { /* good, user supplied timeout value */
option = tmp;
if (timeflag) { /* not the first timeout option */
timeout += i; /* accumulate this timeout value */
} else { /* first timeout option */
timeout = i;
}
} /* if no timeout value is supplied, ignore it */
timeflag = 1; /* enable timeout input */
break;
case 'm': case 'M': /* timeout in minutes */
tmp = xi(option, &i); /* read timeout value (in seconds) */
if (tmp > option) { /* good, user supplied timeout value */
option = tmp;
if (timeflag) { /* not the first timeout option */
timeout += i * 60; /* convert minutes to seconds */
} else { /* first timeout option */
timeout = i;
}
}
timeflag = 1;
break;
case 'q': case 'Q':
quiet = 1; /* disable error message for unexpected input */
break;
default:
cputs("invalid option '"); putch(c); cputs("' ignored\15\n");
}
c = *option;
} /* while */
} else { /* argv[1] is not an option string */
optf = 0;
}
/* now figure out which is the prompt string, which is the expected
** response string
*/
if (argc-optf > 2) { /* expected response string present */
if (!cases) {
strupr(argv[2+optf]); /* convert to upper case if not case sensitive */
}
ex(argv[2+optf], expect);
} else {
expect[0] = 0; /* no expected response string */
}
if (argc-optf > 1) { /* prompt string present */
prompt = argv[1+optf];
} else {
prompt = "? "; /* default prompt string */
}
if (!echo) { /* if no echo then turn off the cursor */
cursor('s'); /* save cursor mode */
signal(SIGINT, ctrlc); /* restore cursor if break key is hit */
cursor('h'); /* hide cursor */
}
do {
cputs(prompt);
/* flush type-ahead if so requested */
if (flead) {
flush_ahead();
}
/* process timeout if necessary */
if (timeflag) {
/* if user selected timeout option without a timeout value, the default
** value of -1 will be used. This means timeout immediately, it won't
** even read type-ahead's in the keyboard buffers.
*/
if (timeout < 0) {
cexit(255, echo);
}
expire = time(NULL) + timeout;
/* if user select timeout with value 0, the keyboard buffer will be
** examined once meaning type-aheads will be read.
*/
while (!kbhit()) { /* while keyboard buffer is empty */
if (time(NULL) >= expire) { /* timeout! */
cexit(255, echo);
}
}
}
c = xgetc(); /* read a key from keyboard */
if (echo) {
xputc(c); /* echo extended character */
cputs("\15\n");
}
if (!cases) { /* not case sensitive, so convert it to UPPER */
if (c < 256) { /* convert only normal ascii */
c = toupper(c);
}
}
/* If no expected string is supplied, then return the character code
** of the key (e.g. A returns 65) in errorlevel. If an extended ascii
** key is pressed (e.g. [F0]) then this will return 0.
*/
if (expect[0] == 0) {
cexit(c, echo);
}
/* search for c in expected response string */
where = istrchr(expect, c);
if (where > -1) { /* found! */
cexit(where + 1, echo);
}
if (!quiet) {
if (!echo) {
cputs("\15\n");
}
cputs("\7unexpected key, please try again\15\n\n");
}
} while (!quiet);
cexit(0, echo); /* means unexpected key press and quiet option set *